#include<iostream>
#include<math.h>
#include<fstream>

#include "stopwatch.cpp"


class Player
{
	public:
	int amount;  
	int ct;
	Player()
	{
		amount=0;
		ct=0;
	}
	int giveMoney()
	{
		if(amount==0) return -1; 
		int current=int(pow(2,ct));
		if(current>amount)
		{
			int temp=amount;
			amount=0;
			return temp;
		}
		else
		{
			amount=amount-current;
			ct++;
			return current; 
		}
	}
	int takeMoney(int money)
	{
		if(amount==0) return -1; 
		amount+=money; 
	}
};


void playGame(int no_of_player, int initial_amt,int gameCnt)
{	
	gameCnt++;
	printf("\nResult for Game:%d\n",gameCnt);
	printf("\t\t%d Player in this game, $%d per Player \n",no_of_player,initial_amt);
	Player *player;
	player=new Player[no_of_player];
	for(int i=0;i<no_of_player;i++)
		player[i].amount=initial_amt;

	int playersLeft=no_of_player;
	int currentPlayer=0;
	int playerLastRecieved=0;
	int exMon=0;
	int monLeft=0;

	while(playersLeft)
	{
		int i=(currentPlayer+1)>=no_of_player?0:(currentPlayer+1);
		int _flag=0;

		//printf("Current Player :%d\n",currentPlayer);
		monLeft=player[currentPlayer].amount;

		while(player[currentPlayer].amount>0)
		{	
			if(i!=currentPlayer)
			{
				if(player[i].amount>0)
				{	
					exMon++;
					int amt=player[currentPlayer].giveMoney();
					if(amt==-1) break;

					//printf("\tPlayer %d: %d\n",i,amt); 

					player[i].takeMoney(amt);
					playerLastRecieved=i;
				}
			}
			else
			{
				_flag=1;
				break;
			}
			i++;
			if(i>=no_of_player) i=0;
		}
		if(_flag==1) break;
		currentPlayer=playerLastRecieved;
		playersLeft--;
	}

	printf("\t\t%d Total money exchanges\n",exMon);
	printf("\t\tLast Player to give money : Player %d with $%d remaining\n",(currentPlayer+1),player[currentPlayer].amount);
	printf("\tPlayer remaining:\n");
	for(int ie=0;ie<no_of_player;ie++)
	{
		if(player[ie].amount!=0)
		{
			printf("\t\tPlayer %d with $%d \n",(ie+1),player[ie].amount);
		}
	}


}
int main()
{	

	fstream file("PROJ1.DAT",ios::in);
	if(!file)
	{
		cout << "error reading file 1\n";
		return (1);
	}
	int rows=73;
	int cols=2;
	int **array;
	array = new int*[rows];
	for(int i=0; i<rows; i++)
	array[i] = new int [cols];
			for(int i=0; i<rows; i++)
			{
				for(int j=0; j<cols; j++)
					file >> array[i][j];
			}
			for(int i=0; i<rows; i++)
			{
				Stopwatch sw;
				sw.start();
				int gameCnt=0;
				playGame(array[i][0],array[i][1],i);
				sw.stop();
				double timeTaken=sw.user();
				printf("Run Time : %lf\n",timeTaken);
			}

	return 0;
}
/*
Results for Game 1
     32 players in this game, $2 per player.
     84 total money exchanges
     Last Player To Give Money: Player 14 with $1 remaining.
  Players Remaining:
     Player 6 with $23
     Player 14 with $1
     Player 16 with $14
     Player 26 with $13
     Player 31 with $13
Average Run Time: 0

*/

/*int openFile(char fileName)
	{
	fstream file;


}
*/
